$(document).ready(function() {
	// 	$(".currentMeal").focus();
	$.getJSON('./utilities/children_model.php', {
		operation : 'get_checkedin'
	}, function(data) {
		checked_in = data;
		for(var i = 0; i < checked_in.length; i++) {
			ci = checked_in[i];
			var new_lunchcard;
			new_lunchcard = new LunchCard(ci['name'], '.' + ci['picture']);
			$(".childContainer").prepend(new_lunchcard.getDiv());
			new_lunchcard.createSlider();
		}
	});

	$('#checkbutton').attr("disabled", "disabled");
	$('#checkbutton').click(selectCheck);
	function selectCheck() {
		var text = $(".currentMeal").val();
		var meal_div = $('<div></div>');
		var meal_span = $('<input class = mealSpan type = text></input>');
		var meal_minus = $('<span > - </span>');
		var meal_fill_all = $('<input class=fillAllButton type = \'button\'  value = \"Fill All\">');

		meal_div.addClass('addMeal');
		meal_div.attr('id', text);

		meal_span.val(text);
		meal_minus.addClass('minus');

		$(".mealEntries").append(meal_div);
		meal_div.append(meal_minus, meal_span, meal_fill_all);
		$(".currentMeal").val("");
		$('#checkbutton').attr("disabled", "disabled");

		$(".currentMeal").focus();
		//<option value = text> text </option>

		var temp = "<option value=\"" + text + "\">" + text + "</option>";
		var meal_option = $(temp);

		// 		$(".foodLabel").html("Amount of food eaten");
		// 		$(".foodLabel").addClass(text);

		$(".mealList").append(meal_option);
		$('.mealListParent').addClass("added").delay(500).queue(function(next) {
			$(this).removeClass("added");
			next();
		});
		//		$(".mealList").val(text);

		// 		$("option[value=\"default\"]").remove();

		//code to add event handler
		$(".minus").click(function() {
			//loop through each kid's container
			var mealText = $(this).parent().children(".mealSpan").val();
			var option = $("option[value=\"" + mealText + "\"]");
			var textBox = $(this).parent().remove();
			option.remove();
			$(document).find('.mealList').change();
			$('.mealListParent').addClass("removed").delay(500).queue(function(next) {
				$(this).removeClass("removed");
				next();
			});
		});

		$('.fillAllButton').click(function() {
			var text = $(this).parent().attr('id');
			$(document).find('.mealList').val(text).change();
			$('.mealListParent').addClass("changed").delay(500).queue(function(next) {
				$(this).removeClass("changed");
				next();
			});
		});
	};


	$('.currentMeal').keypress(function(event) {
		var keycode = (event.keyCode ? event.keyCode : event.which);
		if($('.currentMeal').val() != "") {
			if(keycode == '13') {
				selectCheck();
			}
		}
	});

	$('.currentMeal').keyup(function(event) {
		if($('.currentMeal').val() != "") {
			$('#checkbutton').removeAttr("disabled");
		} else {
			$('#checkbutton').attr("disabled", "disabled");
		}
	});

	$("#btnSubmit").click(function() {
		$("#btnSubmit").attr("disabled", "true");
		$(".slider").slider({
			disabled : true
		});
		$("select").attr("disabled", true);
		$("textarea").attr("disabled", true);
		$("input").attr("disabled", true);
		
		$.getJSON('./utilities/children_model.php', {operation : 'get_checkedin'}, function(data){
          checked_in = data;
          var names_array = new Array();
          for (var i = 0; i < checked_in.length; i++){
            ci = checked_in[i];
            var name = ci['name'];
            names_array.push(name);            
          }
          $.post('./utilities/log_model.php', {operation : 'insert_entry', type : 'lunch_report', 'names[]' : names_array});
    });
		
		setTimeout(function() {
			window.location.href = "./"
		}, 1500);
		$('#toast').toast("You just submitted the Lunch Report");
	});
});
